package com.softstao.guoyu.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.softstao.guoyu.R;
import com.softstao.guoyu.model.me.Address;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;

import java.util.List;

/**
 * Created by vaikus on 2017/6/22.
 */

public class AddressAdapter extends RecycleViewBaseAdapter<Address> {
    private AddressListener addressListener;
    private int preSelected;
    public AddressAdapter(List<Address> datas) {
        super(datas, R.layout.layout_address_list_item);
    }

    @Override
    public void convert(RecycleViewHolder holder, Address address) {
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.itemView.setLayoutParams(layoutParams);
        String address_detail = address.getProvinceName() + address.getCityName() + address.getAreaName() + address.getAddress();
        holder.setText(R.id.name,"接收人：" + address.getName())
                .setText(R.id.mobile,address.getMobile())
                .setText(R.id.address_v,address_detail);
        RadioButton checkbox = holder.getView(R.id.radio_btn);
        if (address.isDefaultX()==true){
            preSelected = holder.getAdapterPosition();
            checkbox.setChecked(true);
            checkbox.setTextColor(mContext.getResources().getColor(R.color.font_pink));
        }else {
            checkbox.setChecked(false);
            checkbox.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        }
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                if (addressListener!=null){
                    addressListener.isDefault(holder.getAdapterPosition());
                }
            }
        });
        holder.getView(R.id.edit_btn).setOnClickListener(v -> {
            if (addressListener!=null){
                addressListener.edit(holder.getAdapterPosition());
            }
        });
        holder.getView(R.id.delete_btn).setOnClickListener(v -> {
            if (addressListener!=null){
                addressListener.delete(holder.getAdapterPosition());
            }
        });
    }

    public interface AddressListener{
        void edit(int position);
        void delete(int position);
        void isDefault(int position);
    }

    public AddressListener getAddressListener() {
        return addressListener;
    }

    public void setAddressListener(AddressListener addressListener) {
        this.addressListener = addressListener;
    }

    public int getPreSelected() {
        return preSelected;
    }

    public void setPreSelected(int preSelected) {
        this.preSelected = preSelected;
    }
}
